package com.gitee.magic.core.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.regex.Pattern;

import com.gitee.magic.core.json.JsonArray;
import com.gitee.magic.core.json.JsonObject;

/**
 * @author start
 */
public class CheckUtils {
	
	public static boolean isNull(Object obj) {
		return obj==null;
	}

	/**
	 * 检测是否JSON对象
	 */
	public static boolean isJsonObject(Object value) {
		return value.getClass().equals(JsonObject.class);
	}
	
	/**
	 * 检测是否JSON数组
	 */
	public static boolean isJsonArray(Object value) {
		return value.getClass().equals(JsonArray.class);
	}
	
	/**
	 * [1,2,3,4,5,7,9]
	 * Long列表
	 * @param value
	 * @return
	 */
	public static boolean isJsonArrayLong(Object value) {
		if(!isJsonArray(value)) {
			return false;
		}
		try {
			JsonArray json=(JsonArray)value;
			Iterator<Object> ids=json.iterator();
			while(ids.hasNext()) {
				String id=String.valueOf(ids.next());
				if(!CheckUtils.isLong(id)) {
					return false;
				}
			}
			return true;
		}catch(Exception e) {
			return false;
		}
	}
	
	/**
	 * 是否为Bit
	 */
	public static boolean isBit(String number) {
		try {
			Integer bit=Integer.parseInt(number);
			return bit==1||bit==0;
		} catch (Exception e) {
			return false;
		}
	}
	
	/**
	 * 是否为boolean
	 * @param number
	 * @return
	 */
	public static boolean isBoolean(String number) {
		return "true".equals(number)||"false".equals(number);
	}
	
	/**
	 * 是否为Short
	 */
	public static boolean isShort(String number) {
		try {
			Short.parseShort(number);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	/**
	 * 是否为整型
	 */
	public static boolean isInteger(String number) {
		try {
			Integer.parseInt(number);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	/**
	 * 是否为长整型
	 */
	public static boolean isLong(String number) {
		try {
			Long.parseLong(number);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	/**
	 * 是否为浮点型
	 */
	public static boolean isFloat(String number) {
		try {
			Float.parseFloat(number);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	/**
	 * 是否为Double
	 */
	public static boolean isDouble(String number) {
		try {
			Double.valueOf(number);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	/**
	 * 检测是否全为数字型
	 */
	public static boolean isNumeric(String str) {
		for (int i = 0; i < str.length(); i++) {
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * 是否为数字
	 */
	public static boolean isNumber(String number) {
		if (StringUtils.isBlank(number) || 
				number.split("\\.").length !=1 || 
				!number.matches("[0-9]+")) {
			return false;
		}
		return true;
	}
	
	/**
	 * 是否为数字有小数
	 */
	public static boolean isDecimal(String number) {
		if (StringUtils.isBlank(number) || 
				number.split("\\.").length != 2 || 
				!number.replace(".", "").matches("[0-9]+")) {
			return false;
		}
		return true;
	}
	
	/**
	 * 检测是否是身份证
	 */
	public static boolean isIdCard(String value) {
		return value.matches("^\\d{15}|^\\d{17}([0-9]|X|x)$");
	}
	
	/**
	 * 电话号码
	 */
	public static boolean isPhone(String phone) {
		phone = StringUtils.nullToStrTrim(phone);
		int length1=11;
		int length2=12;
		if (phone.length() != length1 && phone.length() != length2) {
			return false;
		}
		String regex = "[0-9]+";
		return phone.matches(regex);
	}
	
	/**
	 * 检测移动手机号码
	 */
	public static boolean isMobile(String mobile){
		mobile = StringUtils.nullToStrTrim(mobile);
		int length1=11;
		if (mobile.length() != length1) {
			return false;
		}
		String regex = "^(1[3,4,5,6,7,8,9][0-9])\\d{8}$";
		return mobile.matches(regex);
	}
	
	/**
	 * 检测电子邮件
	 */
	public static boolean isMail(String email){
		if (StringUtils.isEmpty(email)) {
			return false;
		}
		int length1=5;
		email = StringUtils.nullToStrTrim(email);
		if (email.length() < length1) {
			return false;
		}
		String regex = "\\w+(\\.\\w+)*@\\w+(\\.\\w+)+";
		return email.matches(regex);
	}
	
	/**
	 * 检测IP地址
	 */
	public static boolean isIpAddress(String ip){
		if (StringUtils.isEmpty(ip) || StringUtils.isBlank(ip)) {
			return false;
		}
		int length1=7;
		if (ip.length() < length1) {
			return false;
		}
		String ipRegex="[0-9.]+";
		if (!StringUtils.regex(ip, ipRegex)) {
			return false;
		}
		return true;
	}
	
	/**
	 * 检测MAC地址
	 */
	public static boolean isMacAddress(String mac){
		if (StringUtils.isEmpty(mac) || StringUtils.isBlank(mac)) {
			return false;
		}
		int length1=17;
		if (mac.length() != length1) {
			return false;
		}
		return StringUtils.regex(mac,
				"[0-9A-Fa-f]{2}-[0-9A-Fa-f]{2}-[0-9A-Fa-f]{2}-[0-9A-Fa-f]{2}-[0-9A-Fa-f]{2}-[0-9A-Fa-f]{2}");
	}
	
	/**
	 * 检测MD5
	 */
	public static boolean isMd5(String md5){
		if(StringUtils.isEmpty(md5)){
			return false;
		}
		int length1=32;
		if (md5.length() != length1) {
			return false;
		}
		return StringUtils.regex(md5, "[0-9A-Fa-f]+");
	}
	
	/**
	 * 检测SHA1
	 */
	public static boolean isSha1(String sha1){
		if(StringUtils.isEmpty(sha1)){
			return false;
		}
		int length1=40;
		if (sha1.length() != length1) {
			return false;
		}
		return StringUtils.regex(sha1, "[0-9A-Fa-f]+");
	}
	
	/**
	 * 判断是否全为中文字符
	 */
	public static boolean isChinese(String str) {
		if (!StringUtils.isEmpty(str)) {
			for(int i=0;i<str.length();i++){
				char c=str.charAt(i);
				if (!String.valueOf(c).matches("[\\u4e00-\\u9fbb]+")) {
					return false;
				}
			}

		}
		return true;
	}

	/**
	 * 判断是否Base64编码格式
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isBase64(String str) {
		String base64Pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
		return Pattern.matches(base64Pattern, str);
	}
	
	/**
	 * 检测时间格式
	 */
	public static boolean checkTime(String time,String format) {
		try {
			DateFormat formatter = new SimpleDateFormat(format);
			Date date = (Date) formatter.parse(time);
			return time.equals(formatter.format(date));
		} catch (ParseException e) {
			return false;
		}
	}
	
}
